/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.systemui;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.content.res.Configuration;
import android.os.SystemProperties;
import android.util.Log;

import com.unisoc.floatkey.SystemUIFloatKey;
/**
 * Placeholder for any vendor-specific services.
 */
public class VendorServices extends SystemUI {

    private static final String TAG = "VendorServices";
    private int mDensity;
    //UNISOC: Add for bug 1150044
    private int mOrientation;
    private SystemUIFloatKey mSystemUIFloatKey = null;
    //UNISOC: Add for bug 1360717
    private Handler mHandler = new Handler();

    public VendorServices(Context context) {
        super(context);
    }

    @Override
    public void start() {//NOTE: YOU MUST REMOVE FLOATKEY FOR GO.NOTENOTENOTE
        // no-op
        /*SPRD: bug 692486 Assistant touch @{*/
        try {
            if (SystemProperties.getBoolean("ro.product.assistanttouch",true)) {
                final Configuration currentConfig = mContext.getResources().getConfiguration();
                mDensity = currentConfig.densityDpi;
                //UNISOC: Add for bug 1150044
                mOrientation = currentConfig.orientation;
                /*SPRD: bug 692486 Assistant touch @{*/
                /* UNISOC: Modify for bug990337 {@ */
                mSystemUIFloatKey = SystemUIFloatKey.getInstance(mContext);
                /*@}*/
                mSystemUIFloatKey.start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*@}*/
    }

    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        /* UNISOC: Modify for bug 1150044,1360717 @ { */
        if (SystemProperties.getBoolean("ro.product.assistanttouch",true)) {
            final int orientation = newConfig.orientation;
            final int density = newConfig.densityDpi;
            if (density != mDensity) {
                Log.d(TAG, "onDensityChanged:" +mDensity + "->"+density);
                onDensityChanged();
                mDensity = density;
            } else if (orientation != mOrientation){
                Log.d(TAG, "Orientation changed: mOrientation :"+ mOrientation + "->" + orientation);
                mOrientation = orientation;
                mHandler.postDelayed(new Runnable(){
                    public void run() {
                        onDensityChanged();
                    }
                }, 50);
            }
        }
        /* }@ */
    }

    private void onDensityChanged() {
        mSystemUIFloatKey.restart();
    }

}
